--*************ANIMATION SUITE****************--
--**************Property of Hxrmn*****************--
--*************Hxrmn@comcast.net*****************--
--DO NOT RE-DISTRIBUTE WITHOUT HXRMN'S CONSENT--
--********************************************--

if(SERVER) then
	AddCSLuaFile('init.lua')
	AddCSLuaFile('vTrack.lua')
	AddCSLuaFile('vTimeline.lua')
	AddCSLuaFile('vKeyFrame.lua')
end

if(CLIENT) then
	contextOpen = false
	include( 'vTrack.lua' )
	include( 'vTimeline.lua' )
	include( 'vKeyFrame.lua' )
	
	AS_KeyFrameWidth = 10
	
	local lh = 0
	local MouseX = ScrW()/2
	local MouseY = ScrH()/2

	function AddTimeline()
		Timeline = vgui.Create( "vTimeline", AS_Window )
		Timeline:SetPos( 10, 26 )
		Timeline:SetSize(AS_Window:GetWide()-20, Timeline:GetTall())
		lh = lh + 45
	end
	
	function AddPanelList()
		TrackList = vgui.Create( "DPanelList", AS_Window )
		TrackList:EnableVerticalScrollbar()
		TrackList:EnableHorizontal( false )
		TrackList:SetPadding( 10 )
		lh = lh + 26
		TrackList:SetPos( 10, lh )
		TrackList:SetSize( AS_Window:GetWide() - 20, (AS_Window:GetTall()-lh)-60 )
		lh = (AS_Window:GetTall()-lh)-5
	end

	function QuickButton(width, text, func, add, row)
		local btn = vgui.Create( "DButton", AS_Window )
		btn:SetPos(lw, lh+5+(22*row))
		btn:SetSize(width,20)
		btn:SetText(text)
		btn.DoClick = func
		lw = lw + width
		if(add != nil) then
			lw = lw + add
		end
	end

	function AddButtons()
		lw = 10
		QuickButton(120,"Capture + Advance",function() AS_GetAnimator():GetControls():CaptureAndAdvance() end,5,1)
		QuickButton(70,"Apply Key",function() AS_GetAnimator():GetControls():CaptureFrame() end,5,1)
		QuickButton(100,"Add Entity Track",function() AS_GetAnimator():GetControls():AddEnt() end,5,1)
		--QuickButton(70,"Delete Key",function() AS_GetAnimator():GetControls():DeleteKey() end,5,1)
		QuickButton(90,"Delete Tracks",function() AS_GetAnimator():GetControls():Clear() end,25,1)
		lw = 10
		QuickButton(40,"Play",function() AS_GetAnimator():GetControls():PlayX() end,5,2)
		QuickButton(60,"Record",function() AS_GetAnimator():GetControls():Record() end,5,2)
		QuickButton(40,"Stop",function() AS_GetAnimator():GetControls():StopX() end,5,2)
	end

	function AS_UpdateWindow()

	end
	
	function AS_SetFrameCount(c)
		TimeLine:SetFrameCount(c)
	end
	
	function AS_KeyFrameUpdated(track,frame)
		Msg("KeyUpdate\n")
		--Timeline:SetFrame(frame:GetTime(),true)
		/*if(frame == nil) then return end
		for item,t in pairs(TrackList.Items) do
			if(t:GetTrack() == track) then
				t:SetFrame(frame:GetTime(),true)
			end
			break;
		end*/
	end
	
	function AS_TrackUpdated(track)
		for item,t in pairs(TrackList.Items) do
			if(t:GetTrack() == track) then
				t:SetTrack(track)
			end
			break;
		end
	end

	function AS_TrackAdded(track)
		local vtrack = vgui.Create("vTrack",TrackList)
		vtrack:SetTrack(track)
		track:SetTrackPanel(vtrack)
		TrackList:AddItem(vtrack)
	end

	function AS_TrackRemoved(track,id)
		if(track != nil) then
			panel = track:GetTrackPanel()
			if(panel != nil) then
				TrackList:RemoveItem(panel)
			end
		end
		/*TrackList:Clear()

		for k,v in pairs(AS_GetAnimator():GetTracks()) do
			if(v != nil and v.valid == true) then
				Msg("ADDED!!!!!\n")
				AS_TrackAdded(v)
			end
		end*/
	
		TrackList:PerformLayout()
	end
	
	if(AS_Window != nil) then
		AS_Window:Remove()
		AS_Window = nil
	end
	AS_Window = nil

	function AS_OpenWindow()
		lh = 0;
		gui.SetMousePos(MouseX,MouseY)
		--AS_CloseWindow()
		if(AS_Window == nil) then
			AS_Window = vgui.Create( "DFrame" )
				AS_Window:SetSize( ScrW()/1.4, 300 )
				AS_Window:SetPos((ScrW()/2) - AS_Window:GetWide()/2, ScrH()-AS_Window:GetTall()-100)
				AS_Window:SetTitle( "Animation" )
				AS_Window:SetVisible( true )
				AS_Window:SetDeleteOnClose(false)
				AS_Window:SetKeyboardInputEnabled(false)
				AS_Window:MakePopup()
			AddTimeline()
			AddPanelList()
			AddButtons()
		end
		Timeline:ResetFrameCount()
		AS_Window:SetVisible(true)
		--return true
	end
	--hook.Add("ScoreboardShow", "AS_OpenWindow", AS_OpenWindow)

	function AS_CloseWindow()
		Timeline:OnMouseReleased()
		MouseX = gui.MouseX()
		MouseY = gui.MouseY()
		if(AS_Window != nil) then
			AS_Window:SetVisible(false)
		end
		--return true
	end
	--hook.Add("ScoreboardHide", "AS_CloseWindow", AS_CloseWindow)
	
	function AS_PlayerBindPress(ply, bind, pressed)
		Msg(bind .. " " .. tostring(pressed) .. "\n")
		if(bind == "+menu_context") then
			contextOpen = pressed
			if(contextOpen and GetConVarString("gmod_toolmode") == "animation") then
				AS_OpenWindow()
			else
				AS_CloseWindow()
			end
		end
	end
	--hook.Add("PlayerBindPress","AS_PlayerBindPress",AS_PlayerBindPress)
	
	hook.Add("OnContextMenuOpen","OpenAnimatorWindow",AS_OpenWindow)
	hook.Add("OnContextMenuClose","CloseAnimatorWindow",AS_CloseWindow)
	
	timer.Simple(.02,AS_OpenWindow)
	timer.Simple(.04,AS_CloseWindow)
end